<template>
  <div>
    <div style="padding: 10px; width: 100%">
      <div style="margin: 10px 0">
        <el-input
          placeholder="请输入课程名称"
          v-model="search"
          style="width: 200px"
          @blur="blurUsername"
        >
          <template #append>
            <el-button icon="el-icon-search" @click="load"></el-button>
          </template>
        </el-input>

        <el-button type="primary" style="margin-left: 20px" @click="add"
          >添加</el-button
        >
      </div>
      <el-table :data="tableData" border stripe style="width: 60%">
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column prop="id" label="课程编号"  sortable />
        <el-table-column prop="name" label="课程名称" />

        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              @click="handleEdit(scope.row)"
              size="mini"
              type="primary"
              plain
              >编辑</el-button
            >
            <el-button
              @click="handleDelete(scope.row.id)"
              size="mini"
              type="danger"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 10px 0">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <el-dialog
        title="添加课程"
        v-model="dialogVisible"
        width="30%"
        :before-close="dialogClose"
      >
        <el-form ref="form" :model="form" label-width="80px" :rules="rules">
          <el-form-item label="课程编号" prop="id">
            <el-input v-model.number="form.id" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="课程名称" prop="name">
            <el-input v-model="form.name" style="width: 200px"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCourse">确 定</el-button>
          </span>
        </template>
      </el-dialog>
      <el-dialog title="修改课程" v-model="updateVisible" width="30%">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="课程编号">
            <el-input v-model="form.id" style="width: 200px" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="课程名称">
            <el-input v-model="form.name" style="width: 200px"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="updateVisible = false">取 消</el-button>
            <el-button type="primary" @click="update">确 定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import request from "../../utils/request";
export default {
  name: "Course",
  data() {
    return {
      search: "",
      form: {},
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      updateVisible: false,

       rules: {
          id: 
          [
            { required: true, message: '请输入编号', trigger: 'blur' },
             { type: 'number', message: '年龄必须为数字值'}
          ],
          name:
          [
            { required: true, message: '请输入课程名称', trigger: 'blur' },
          ]
       }
    };
  },
  // 创建时
  created() {
    this.load();
  },
  methods: {
    // 分页查询
    load() {
      request
        .get("/course", {
          params: {
            PageNum: this.currentPage,
            PageSize: this.pageSize,
            search: this.search,
          },
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
        });
    },
    // 输入框失去焦点
    blurUsername() {
      this.load();
    },
    // 新增按钮
    add() {
      this.dialogVisible = true;
      this.form = {};
    },
    // 添加课程
    addCourse() {
       this.$refs.form.validate((valid) => {
         if (valid) {
            request.post("/course/addCourse", this.form).then((res) => {
        console.log(res);
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "添加成功",
          });
          this.load();
           this.dialogClose();
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
       
      });
         }
       })
      
    },
    // 删除课程
    handleDelete(id) {
      console.log(id);
      this.$confirm("是否删除该课程?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          request.delete("/course/delete/" + id).then((res) => {
            if (res.code === "0") {
              this.$message({
                type: "success",
                message: "删除成功",
              });
            } else {
              this.$message({
                type: "error",
                message: res.msg(),
              });
            }
            this.load();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 更新课程
    update() {
      request.put("/course/updateCourse", this.form).then((res) => {
        console.log(res);
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "更新成功",
          });
        } else {
          this.$message({
            type: "error",
            message: "更新失败",
          });
        }
        this.load();
        this.updateVisible = false;
      });
    },

    exportExcel(){
      request.post("/course/export").then((res) => {
      });
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.updateVisible = true;
    },
    // 取消按钮
    dialogClose() {
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    //改变当前每页的个数触发
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    //改变当前页码触发
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.load();
    },
  },
};
</script>

